// 小程序开发api接口统一配置
// 如果你的域名是： https://www.baidu.com/cn 那么这里只要填写 cn
let subDomain = '/xiaochengxu'  // 子域名,没有就等于''
const API_BASE_URL = 'https://zmj.aneasyhome.com'  // 主域名
 
const request = (url, method, data) => {
  let _url = API_BASE_URL + subDomain  + url
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: method,
      data: data,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete(aaa) {
        // 加载完成
      }
    })
  })
}
 
/**
 * 小程序的promise没有finally方法，自己扩展下
 */
Promise.prototype.finally = function (callback) {
  var Promise = this.constructor;
  return this.then(
    function (value) {
      Promise.resolve(callback()).then(
        function () {
          return value;
        }
      );
    },
    function (reason) {
      Promise.resolve(callback()).then(
        function () {
          throw reason;
        }
      );
    }
  );
}
 
module.exports = {
  request,
  // 登录接口
  denglu: data => request('/login.php','post', data),
  // 首页列表接口
  banner: data => request('/a_banner.php','post', data),
  // 详情接口
  category: (data) => request('/a_category.php','post', data),
  // 商品分类
  product_list: (data) => request('/b_category.php','post', data),
  // 搜索、商品详情
  product_search: (data) => request('/goods.php','post', data),
  // 添加购物车
  add_Shopping: (data) => request('/c_cart.php','post', data),
  // 修改购物车
  modify_Shopping: (data) => request('/c_cart.php','post', data),
  // 查询购物车
  lookup_Shopping: (data) => request('/c_cart.php','post', data),
  // 删除购物车
  remove_Shopping: (data) => request('/c_cart.php','post', data),
  // 地区接口
  address: (data) => request('/all/address.php','post', data),
  // 时间接口
  date: (data) => request('/all/date.php','post', data),
  // 收货地址(增、删、查、改)
  address_shouhuo: (data) => request('/e_address.php','post', data),
  // 生成订单接口
  commodity_order: (data) => request('/d_order.php','post', data),
  // 支付接口
  payment: (data) => request('/wxpay/index.php','post', data),
}